{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.10 64-bit ('test': conda)"
  },
  "interpreter": {
   "hash": "f9068beaa27b1717c309acb10300fb2603990c09df67c94bbef42553159f9e1a"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import joblib\n",
    "class AIrecommender :\n",
    "\n",
    "    ad_sim = joblib.load(\"model/ad_sim.model\")\n",
    "    summary_sim = joblib.load(\"model/summary_sim.model\")\n",
    "    tags_sim = joblib.load(\"model/tags_sim.model\")\n",
    "    movieid_to_index = pd.read_csv(\"model/movieid_to_index.csv\")\n",
    "    movieid_to_index = pd.Series(movieid_to_index.index, index=movieid_to_index['id'])\n",
    "    title_to_index = pd.read_csv(\"model/title_to_index.csv\")\n",
    "    title_to_index = pd.Series(title_to_index.index, index=title_to_index['title'])\n",
    "        \n",
    "    \n",
    "    def title_to_movieid(title):\n",
    "        print(AIrecommender.title_to_index[title])\n",
    "        return AIrecommender.movieid_to_index.index[AIrecommender.title_to_index[title]]\n",
    "    \n",
    "    def recommend_tags_sim(index, start, num) :\n",
    "        sim_scores = list(enumerate(AIrecommender.tags_sim[index]))\n",
    "        sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)\n",
    "        return sim_scores[start:start+num]\n",
    "\n",
    "    def recommend_ad_sim(index, start, num) :\n",
    "        sim_scores = list(enumerate(AIrecommender.ad_sim[index]))\n",
    "        sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)\n",
    "        return sim_scores[start:start+num]\n",
    "\n",
    "    def recommend_summary_sim(index, start, num) :\n",
    "        sim_scores = list(enumerate(AIrecommender.summary_sim[index]))\n",
    "        sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)\n",
    "        return sim_scores[start:start+num]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_model_element(model, index, columns):\n",
    "    sim_scores = list(enumerate(model[index]))\n",
    "    return sim_scores[columns][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def recommend_by_all_model(movieId, start, num) : \n",
    "    index = AIrecommender.movieid_to_index[movieId]\n",
    "    sim_res_list = []\n",
    "    tags_sim_res = AIrecommender.recommend_tags_sim(index, start, num)\n",
    "    sim_res_list.append(tags_sim_res)\n",
    "    ad_sim_res = AIrecommender.recommend_ad_sim(index, start, num)\n",
    "    sim_res_list.append(ad_sim_res)\n",
    "    summary_sim_res = AIrecommender.recommend_summary_sim(index, start, num)\n",
    "    sim_res_list.append(summary_sim_res)\n",
    "    movie_id_list = []\n",
    "    for res in tags_sim_res:\n",
    "        temp_dict = {}\n",
    "        temp_dict['movie_id'] = AIrecommender.movieid_to_index.index[res[0]]\n",
    "        temp_dict['sim_score'] = (res[1] + get_model_element(AIrecommender.ad_sim, index, res[0]) + get_model_element(AIrecommender.summary_sim, index, res[0]))/3\n",
    "        movie_id_list.append(temp_dict)\n",
    "    for res in ad_sim_res:\n",
    "        temp_dict = {}\n",
    "        temp_dict['movie_id'] = AIrecommender.movieid_to_index.index[res[0]]\n",
    "        temp_dict['sim_score'] = (res[1] + get_model_element(AIrecommender.tags_sim, index, res[0]) + get_model_element(AIrecommender.summary_sim, index, res[0]))/3\n",
    "        movie_id_list.append(temp_dict)\n",
    "    for res in summary_sim_res:\n",
    "        temp_dict = {}\n",
    "        temp_dict['movie_id'] = AIrecommender.movieid_to_index.index[res[0]]\n",
    "        temp_dict['sim_score'] = (res[1] + get_model_element(AIrecommender.ad_sim, index, res[0]) + get_model_element(AIrecommender.tags_sim, index, res[0]))/3\n",
    "        movie_id_list.append(temp_dict)\n",
    "\n",
    "        \n",
    "    res_list = sorted(movie_id_list, key=lambda x: x['sim_score'], reverse=True)\n",
    "\n",
    "    for s_res in sim_res_list:\n",
    "        print(s_res)\n",
    "        temp = [i[0] for i in s_res]\n",
    "        movie_id_list.append(temp)\n",
    "        print(AIrecommender.title_to_index[temp])\n",
    "    return res_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def recommend_by_tags_model(movieId, start, num) : \n",
    "    index = AIrecommender.movieid_to_index[movieId]\n",
    "    tags_sim_res = AIrecommender.recommend_tags_sim(index, start, num)\n",
    "    res_list = []\n",
    "    for res in tags_sim_res:\n",
    "        temp_dict = {}\n",
    "        temp_dict['movie_id'] = AIrecommender.movieid_to_index.index[res[0]]\n",
    "        temp_dict['sim_score'] = res[1]\n",
    "        res_list.append(temp_dict)\n",
    "    return res_list\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def recommend_by_ad_model(movieId, start, num) : \n",
    "    index = AIrecommender.movieid_to_index[movieId]\n",
    "    ad_sim_res = AIrecommender.recommend_ad_sim(index, start, num)\n",
    "    res_list = []\n",
    "    for res in ad_sim_res:\n",
    "        temp_dict = {}\n",
    "        temp_dict['movie_id'] = AIrecommender.movieid_to_index.index[res[0]]\n",
    "        temp_dict['sim_score'] = res[1]\n",
    "        res_list.append(temp_dict)\n",
    "    return res_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def recommend_by_summary_model(movieId, start, num) : \n",
    "    index = AIrecommender.movieid_to_index[movieId]\n",
    "    summary_sim_res = AIrecommender.recommend_summary_sim(index, start, num)\n",
    "    res_list = []\n",
    "    for res in summary_sim_res:\n",
    "        temp_dict = {}\n",
    "        temp_dict['movie_id'] = AIrecommender.movieid_to_index.index[res[0]]\n",
    "        temp_dict['sim_score'] = res[1]\n",
    "        res_list.append(temp_dict)\n",
    "    return res_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "8202\n",
      "1292523\n",
      "[(8683, 0.7378647873726218), (8505, 0.7035264706814485), (8434, 0.5892556509887895), (9030, 0.5892556509887895), (8251, 0.5555555555555556), (8486, 0.5555555555555556), (8052, 0.5270462766947299), (8330, 0.5270462766947299), (8344, 0.5270462766947299), (8904, 0.5270462766947299), (8302, 0.502518907629606), (8402, 0.502518907629606), (8469, 0.502518907629606), (8706, 0.502518907629606), (8931, 0.502518907629606), (9035, 0.502518907629606), (15026, 0.502518907629606), (1133, 0.4714045207910316), (1144, 0.4714045207910316), (1495, 0.4714045207910316)]\n",
      "title\n",
      "侏罗纪公园3           8683\n",
      "侏罗纪公园2：失落的世界     8505\n",
      "侏罗纪世界            8434\n",
      "侏罗纪世界2           9030\n",
      "异形2              8251\n",
      "时空大挪移            8486\n",
      "2001太空漫游         8052\n",
      "星河战队             8330\n",
      "2010             8344\n",
      "终结者：创世纪          8904\n",
      "深渊               8302\n",
      "星际旅行8：第一类接触      8402\n",
      "星际旅行2：可汗怒吼       8469\n",
      "星际旅行3：石破天惊       8706\n",
      "星际旅行5：终极先锋       8931\n",
      "世界之战             9035\n",
      "机器战警3           15026\n",
      "未来世界             1133\n",
      "西部世界             1144\n",
      "黑暗心灵             1495\n",
      "dtype: int64\n",
      "[(8505, 0.5714285714285713), (1078, 0.28571428571428564), (6852, 0.28571428571428564), (6965, 0.28571428571428564), (7005, 0.28571428571428564), (7024, 0.28571428571428564), (8059, 0.28571428571428564), (8387, 0.28571428571428564), (8446, 0.28571428571428564), (8449, 0.28571428571428564), (8736, 0.28571428571428564), (8844, 0.28571428571428564), (8998, 0.28571428571428564), (9034, 0.28571428571428564), (9035, 0.28571428571428564), (9817, 0.28571428571428564), (9908, 0.28571428571428564), (10077, 0.28571428571428564), (10184, 0.28571428571428564), (10237, 0.28571428571428564)]\n",
      "title\n",
      "侏罗纪公园2：失落的世界     8505\n",
      "少数派报告            1078\n",
      "猫鼠游戏             6852\n",
      "华盛顿邮报            6965\n",
      "勇者无惧             7005\n",
      "间谍之桥             7024\n",
      "头号玩家             8059\n",
      "夺宝奇兵2            8387\n",
      "丁丁历险记            8446\n",
      "大白鲨              8449\n",
      "夺宝奇兵4            8736\n",
      "横冲直撞大逃亡          8844\n",
      "铁钩船长             8998\n",
      "圆梦巨人             9034\n",
      "世界之战             9035\n",
      "辛德勒的名单           9817\n",
      "拯救大兵瑞恩           9908\n",
      "人工智能            10077\n",
      "E.T. 外星人        10184\n",
      "紫色              10237\n",
      "dtype: int64\n",
      "[(7781, 0.32603059954149), (8505, 0.3228304558021829), (4875, 0.3104031624071741), (5720, 0.3020724451149982), (9691, 0.27547677020808703), (9494, 0.2490908516546356), (9030, 0.2314817625241202), (7980, 0.22586447368366053), (2883, 0.21361279870623237), (9769, 0.18282211594067602), (3609, 0.1652310087382911), (8634, 0.16110591219682313), (4081, 0.15559885516980473), (9320, 0.14763638006352706), (5702, 0.1329748150234417), (8683, 0.1326895029042657), (8489, 0.12347701080512197), (8434, 0.1156327223572498), (16794, 0.11536547796280691), (3751, 0.11383544666331154)]\n",
      "title\n",
      "你看起来好像很好吃         7781\n",
      "侏罗纪公园2：失落的世界      8505\n",
      "赛文奥特曼 地球星人的大地     4875\n",
      "恐龙末日              5720\n",
      "我的宠物恐龙            9691\n",
      "一声惊雷              9494\n",
      "侏罗纪世界2            9030\n",
      "猪猪侠大电影·恐龙日记       7980\n",
      "舍与得               2883\n",
      "史前一亿年             9769\n",
      "玩具总动员：遗忘的时光       3609\n",
      "恐龙当家              8634\n",
      "派对焦点              4081\n",
      "瑜伽熊               9320\n",
      "维龙加               5702\n",
      "侏罗纪公园3            8683\n",
      "被时间遗忘的土地          8489\n",
      "侏罗纪世界             8434\n",
      "有关时间旅行的热门问题      16794\n",
      "玩具总动员：派对恐龙        3751\n",
      "dtype: int64\n"
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[{'movie_id': 1293702, 'sim_score': 0.5325951659707342},\n",
       " {'movie_id': 1293702, 'sim_score': 0.5325951659707342},\n",
       " {'movie_id': 1293702, 'sim_score': 0.5325951659707342},\n",
       " {'movie_id': 1304643, 'sim_score': 0.3416192134129932},\n",
       " {'movie_id': 1304643, 'sim_score': 0.3416192134129932},\n",
       " {'movie_id': 26416062, 'sim_score': 0.2735791378376366},\n",
       " {'movie_id': 26416062, 'sim_score': 0.2735791378376366},\n",
       " {'movie_id': 1309044, 'sim_score': 0.26673294206848475},\n",
       " {'movie_id': 1309044, 'sim_score': 0.26673294206848475},\n",
       " {'movie_id': 1301434, 'sim_score': 0.2523729355017724},\n",
       " {'movie_id': 1294941, 'sim_score': 0.24712613322099927},\n",
       " {'movie_id': 1302827, 'sim_score': 0.24432044377086326},\n",
       " {'movie_id': 1428051, 'sim_score': 0.24338624338624334},\n",
       " {'movie_id': 1294638, 'sim_score': 0.24338624338624334},\n",
       " {'movie_id': 1297689, 'sim_score': 0.23578376902335652},\n",
       " {'movie_id': 10440138, 'sim_score': 0.23496279111534646},\n",
       " {'movie_id': 10440138, 'sim_score': 0.23496279111534646},\n",
       " {'movie_id': 1307266, 'sim_score': 0.22357595767013982},\n",
       " {'movie_id': 4920389, 'sim_score': 0.2135526674307108},\n",
       " {'movie_id': 1295124, 'sim_score': 0.20899176671914343},\n",
       " {'movie_id': 1292849, 'sim_score': 0.20462640199503435},\n",
       " {'movie_id': 2028669, 'sim_score': 0.2006473505770412},\n",
       " {'movie_id': 1305487, 'sim_score': 0.19574187676401644},\n",
       " {'movie_id': 1298692, 'sim_score': 0.1914631401030328},\n",
       " {'movie_id': 1297569, 'sim_score': 0.1860896855362487},\n",
       " {'movie_id': 1293792, 'sim_score': 0.1851851851851852},\n",
       " {'movie_id': 1796752, 'sim_score': 0.1817046773869686},\n",
       " {'movie_id': 5998300, 'sim_score': 0.17925823517017134},\n",
       " {'movie_id': 3338862, 'sim_score': 0.17658862824744126},\n",
       " {'movie_id': 1295384, 'sim_score': 0.17645522261827698},\n",
       " {'movie_id': 1292226, 'sim_score': 0.17608601177315306},\n",
       " {'movie_id': 1293572, 'sim_score': 0.17568209223157663},\n",
       " {'movie_id': 1298386, 'sim_score': 0.17380551536993383},\n",
       " {'movie_id': 25875021, 'sim_score': 0.17380551536993383},\n",
       " {'movie_id': 1294503, 'sim_score': 0.16970456490322916},\n",
       " {'movie_id': 1300371, 'sim_score': 0.16935857024587928},\n",
       " {'movie_id': 1295802, 'sim_score': 0.1693121693121693},\n",
       " {'movie_id': 1299560, 'sim_score': 0.16852270466370112},\n",
       " {'movie_id': 1299888, 'sim_score': 0.167506302543202},\n",
       " {'movie_id': 1293316, 'sim_score': 0.167506302543202},\n",
       " {'movie_id': 1294015, 'sim_score': 0.167506302543202},\n",
       " {'movie_id': 1302570, 'sim_score': 0.167506302543202},\n",
       " {'movie_id': 3026402, 'sim_score': 0.16634996065250465},\n",
       " {'movie_id': 27043980, 'sim_score': 0.1658996641434364},\n",
       " {'movie_id': 1299964, 'sim_score': 0.15713484026367722},\n",
       " {'movie_id': 1297907, 'sim_score': 0.15713484026367722},\n",
       " {'movie_id': 26288143, 'sim_score': 0.15713484026367722},\n",
       " {'movie_id': 5041021, 'sim_score': 0.1405047578394284},\n",
       " {'movie_id': 25908051, 'sim_score': 0.1376536602199526},\n",
       " {'movie_id': 26990609, 'sim_score': 0.13582074829291998},\n",
       " {'movie_id': 6875863, 'sim_score': 0.12397480762490502},\n",
       " {'movie_id': 3233636, 'sim_score': 0.11084548182397891},\n",
       " {'movie_id': 4848115, 'sim_score': 0.10867686651383},\n",
       " {'movie_id': 25815426, 'sim_score': 0.094360712978683},\n",
       " {'movie_id': 25662330, 'sim_score': 0.0889033220936386},\n",
       " {'movie_id': 2046839, 'sim_score': 0.07773886938685494},\n",
       " {'movie_id': 11599090, 'sim_score': 0.07722885895368982},\n",
       " {'movie_id': 35026731, 'sim_score': 0.07528815789455351},\n",
       " {'movie_id': 30390531, 'sim_score': 0.07120426623541079},\n",
       " {'movie_id': 25876119, 'sim_score': 0.04432493834114723}]"
      ]
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "a = AIrecommender.title_to_movieid(\"侏罗纪公园\")\n",
    "print(a)\n",
    "recommend_by_all_model(movieId=a, start=1, num=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[{'movie_id': 1304643, 'sim_score': 0.7378647873726218},\n",
       " {'movie_id': 1293702, 'sim_score': 0.7035264706814485},\n",
       " {'movie_id': 10440138, 'sim_score': 0.5892556509887895},\n",
       " {'movie_id': 26416062, 'sim_score': 0.5892556509887895},\n",
       " {'movie_id': 1293792, 'sim_score': 0.5555555555555556},\n",
       " {'movie_id': 1297569, 'sim_score': 0.5555555555555556},\n",
       " {'movie_id': 1292226, 'sim_score': 0.5270462766947299},\n",
       " {'movie_id': 1295384, 'sim_score': 0.5270462766947299},\n",
       " {'movie_id': 1293572, 'sim_score': 0.5270462766947299},\n",
       " {'movie_id': 3338862, 'sim_score': 0.5270462766947299},\n",
       " {'movie_id': 1299560, 'sim_score': 0.502518907629606},\n",
       " {'movie_id': 1299888, 'sim_score': 0.502518907629606},\n",
       " {'movie_id': 1293316, 'sim_score': 0.502518907629606},\n",
       " {'movie_id': 1294015, 'sim_score': 0.502518907629606},\n",
       " {'movie_id': 1302570, 'sim_score': 0.502518907629606},\n",
       " {'movie_id': 1309044, 'sim_score': 0.502518907629606},\n",
       " {'movie_id': 1300371, 'sim_score': 0.502518907629606},\n",
       " {'movie_id': 1299964, 'sim_score': 0.4714045207910316},\n",
       " {'movie_id': 1297907, 'sim_score': 0.4714045207910316},\n",
       " {'movie_id': 26288143, 'sim_score': 0.4714045207910316}]"
      ]
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "source": [
    "recommend_by_tags_model(movieId=a, start=1, num=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[{'movie_id': 1293702, 'sim_score': 0.5714285714285713},\n",
       " {'movie_id': 1297689, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 1305487, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 26990609, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 1295802, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 25908051, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 4920389, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 1301434, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 2028669, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 1294941, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 1428051, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 1298386, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 1298692, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 25875021, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 1309044, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 1295124, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 1292849, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 1302827, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 1294638, 'sim_score': 0.28571428571428564},\n",
       " {'movie_id': 1294503, 'sim_score': 0.28571428571428564}]"
      ]
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "source": [
    "recommend_by_ad_model(movieId=a, start=1, num=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[{'movie_id': 4848115, 'sim_score': 0.32603059954149},\n",
       " {'movie_id': 1293702, 'sim_score': 0.3228304558021829},\n",
       " {'movie_id': 5041021, 'sim_score': 0.3104031624071741},\n",
       " {'movie_id': 5998300, 'sim_score': 0.3020724451149982},\n",
       " {'movie_id': 27043980, 'sim_score': 0.27547677020808703},\n",
       " {'movie_id': 1307266, 'sim_score': 0.2490908516546356},\n",
       " {'movie_id': 26416062, 'sim_score': 0.2314817625241202},\n",
       " {'movie_id': 35026731, 'sim_score': 0.22586447368366053},\n",
       " {'movie_id': 30390531, 'sim_score': 0.21361279870623237},\n",
       " {'movie_id': 3026402, 'sim_score': 0.18282211594067602},\n",
       " {'movie_id': 25815426, 'sim_score': 0.1652310087382911},\n",
       " {'movie_id': 6875863, 'sim_score': 0.16110591219682313},\n",
       " {'movie_id': 25662330, 'sim_score': 0.15559885516980473},\n",
       " {'movie_id': 3233636, 'sim_score': 0.14763638006352706},\n",
       " {'movie_id': 25876119, 'sim_score': 0.1329748150234417},\n",
       " {'movie_id': 1304643, 'sim_score': 0.1326895029042657},\n",
       " {'movie_id': 1796752, 'sim_score': 0.12347701080512197},\n",
       " {'movie_id': 10440138, 'sim_score': 0.1156327223572498},\n",
       " {'movie_id': 2046839, 'sim_score': 0.11536547796280691},\n",
       " {'movie_id': 11599090, 'sim_score': 0.11383544666331154}]"
      ]
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "source": [
    "recommend_by_summary_model(movieId=a, start=1, num=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}